package com.warmlight.voicepacket.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.warmlight.voicepacket.MyPlaylistEditActivity;
import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.base.MBaseAdapter;
import com.warmlight.voicepacket.data.UserCenterData;

import java.util.List;

/**
 * 用户登录后的播单列表adapter
 */
public class MyCoverListOnNetAdapter extends MBaseAdapter<UserCenterData.ListBean> {

    private boolean isOther;

    public MyCoverListOnNetAdapter(Context context, List<UserCenterData.ListBean> list) {
        super(context, list);
    }

    public MyCoverListOnNetAdapter(Context context, List<UserCenterData.ListBean> list,boolean isOther) {
        super(context, list);
        this.isOther = isOther;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        final MyCoverListOnNetAdapter.ViewHolder holder;
        if (convertView != null) {
            holder = (MyCoverListOnNetAdapter.ViewHolder) convertView.getTag();
        } else {
            convertView = View.inflate(mContext, R.layout.item_myplaylist, null);
            holder = new MyCoverListOnNetAdapter.ViewHolder();
            holder.tv_myplaylist_title = (TextView) convertView.findViewById(R.id.tv_myplaylist_title);
            holder.tv_myplaylist_count = (TextView) convertView.findViewById(R.id.tv_myplaylist_count);
            holder.iv_myplaylist_edit = (ImageView) convertView.findViewById(R.id.iv_myplaylist_edit);
            holder.iv_myplaylist = (ImageView) convertView.findViewById(R.id.iv_myplaylist);
            holder.iv_cover = (ImageView) convertView.findViewById(R.id.iv_cover);
            convertView.setTag(holder);
        }

        final UserCenterData.ListBean data = mList.get(position);
        holder.tv_myplaylist_title.setText(data.getName());
        holder.tv_myplaylist_count.setText(data.getAudio_num()+"个");
        if ("我的最爱".equals(data.getName())){
            holder.iv_cover.setVisibility(View.VISIBLE);
        }else{
            holder.iv_cover.setVisibility(View.GONE);
        }

        String avator = data.getAvator();
        RequestOptions options = new RequestOptions();
        options.placeholder(R.drawable.icon_iv_fail);
        Glide.with(mContext).asBitmap().load(avator).apply(options).into(holder.iv_myplaylist);
//        if("0".equals(data.getAudio_num())){
//            Glide.with(mContext).load(R.drawable.icon_iv_fail).asBitmap().dontAnimate().into(holder.iv_myplaylist);
//        }else{
////            List<DBFloatPlayListDetail> listAudios = DBHelper.getInstance(mContext).queryAudiosByListId(data.get_id());
//            List<DBFloatPlayListDetail> listAudios =  DBFloatPlayListDetailDaoHelp.queryBuilder(mContext,data.getId()+"");
//            if(listAudios!=null && listAudios.size()>0){
//                String url = listAudios.get(0).getAudio_ps();
//                Glide.with(mContext).load(TextUtils.isEmpty(url) ? R.drawable.icon_iv_fail : url).asBitmap().dontAnimate().into(holder.iv_myplaylist);
//            }else {
//                Glide.with(mContext).load(R.drawable.icon_iv_fail).asBitmap().dontAnimate().into(holder.iv_myplaylist);
//            }
//
//        }

        if (isOther){
            holder.iv_myplaylist_edit.setVisibility(View.INVISIBLE);
        }else {
            //如果播单名字是"我的最爱"则隐藏"更多"点点点按钮
            if ("我的最爱".equals(data.getName().trim())) {
                holder.iv_myplaylist_edit.setVisibility(View.INVISIBLE);
            } else {
                holder.iv_myplaylist_edit.setVisibility(View.VISIBLE);
            }

            holder.iv_myplaylist_edit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(mContext, MyPlaylistEditActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("cover_net", data);
                    intent.putExtras(bundle);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    //我的播单条目更多
//                MobclickAgent.onEvent(mContext.getApplicationContext(), MobClickHelper.CLOT_FEED_MORE,((DBFloatPlaylist) mList.get(position)).getList_name());
//                HashMap<String,Object> temp = new HashMap<>();
//                temp.put("我的播单条目更多",((DBFloatPlaylist) mList.get(position)).getList_name().toString());
//                ZhugeSDK.getInstance().track(mContext.getApplicationContext(),MobClickHelper.CLOT_FEED_MORE,temp);

                    mContext.startActivity(intent);

                }
            });
        }

        return convertView;
    }

    private class ViewHolder {
        ImageView iv_myplaylist;
        ImageView iv_myplaylist_edit;
        TextView tv_myplaylist_title;
        TextView tv_myplaylist_count;
        ImageView iv_cover;
    }
}
